package xin.clips.web.controller.gy;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.core.domain.entity.SysDictData;
import xin.clips.common.enums.gy.DeviceStatusEnum;
import xin.clips.common.enums.gy.DeviceTypeEnum;
import xin.clips.common.utils.DictUtils;
import xin.clips.common.utils.StringUtils;
import xin.clips.system.domain.gy.GyDevice;
import xin.clips.system.domain.gy.GyMachineDayProduction;
import xin.clips.system.domain.gy.GyMachineDayRuntime;
import xin.clips.system.dto.*;
import xin.clips.system.mapper.gy.GyMachineDayRuntimeMapper;
import xin.clips.system.service.ISysDictDataService;
import xin.clips.system.service.gy.IGyArmParamService;
import xin.clips.system.service.gy.IGyDeviceService;
import xin.clips.system.service.gy.IGyMachineParamService;
import xin.clips.system.utils.ByteUtils;
import xin.clips.system.utils.LogUtil;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.*;

@RestController
@RequestMapping("/gather")
public class GatherController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(GatherController.class);
    private static final Integer PORT = 6667;


    /**
     * 设备状态显示数据
     *
     * @return
     */
    @RequestMapping(value = "/location")
    public AjaxResult location() {
        HashMap<String, Double> map = new HashMap<>();
        try {
            byte[] msg = new byte[1024];
            DatagramSocket udp = new DatagramSocket(PORT);
            DatagramPacket packet = new DatagramPacket(msg, 1024);
            udp.receive(packet);
            packet.getData();
            log.info(msg.length + "接收到的报文{}", msg);
            byte[] data = Arrays.copyOfRange(msg, 12, 100);
            byte[] x = Arrays.copyOfRange(data, 8, 16);
            double xValue = ByteUtils.bytes2Double(x);
            log.info("robot_pose:x=" + xValue + " ");
            byte[] y = Arrays.copyOfRange(data, 16, 24);
            double yValue = ByteUtils.bytes2Double(y);
            log.info("robot_pose:y=" + yValue);

            map.put("xValue",xValue);
            map.put("yValue",yValue);
        } catch (Exception e) {
            logger.info(e.getMessage());
            LogUtil.dumpException(e);
        }
        return AjaxResult.success(map);
    }



}
